package com.tushu.service;

import com.tushu.entity.Book;
import com.tushu.util.JdbcUtil;

import java.util.List;

public class BookService {

    /**
     * 列表
     * @param page
     * @param rows
     * @param obj
     * @return
     */
    public List<Book> getListAll(int page, int rows, Book obj) {
        String sql="select * from book where 1=1 ";
        if(obj.getBname()!=null&&!obj.getBname().equals("")){
            sql+=" and bname like '%"+obj.getBname()+"%' ";
        }
        if(obj.getBauthor()!=null&&!obj.getBauthor().equals("")){
            sql+=" and bauthor like '%"+obj.getBauthor()+"%' ";
        }
        sql+=" limit "+page+","+rows;
        List<Book> list= JdbcUtil.executeQuery(sql,null,Book.class);
        return list;
    }

    /**
     * 总数
     * @param obj
     * @return
     */
    public int getListCount(Book obj) {
        String sql="select * from book where 1=1 ";
        if(obj.getBname()!=null&&!obj.getBname().equals("")){
            sql+=" and bname like '%"+obj.getBname()+"%' ";
        }
        if(obj.getBauthor()!=null&&!obj.getBauthor().equals("")){
            sql+=" and bauthor like '%"+obj.getBauthor()+"%' ";
        }
        List<Book> list= JdbcUtil.executeQuery(sql,null,Book.class);
        return list.size();
    }


    /**
     * 新增
     * @param obj
     * @return
     */
    public int add(List<Object> obj) {
        String sql="insert into book(bname,bauthor,bpublish,bprice,bdate,bsbn) values(?,?,?,?,?,?) ";
        int count= JdbcUtil.executeUpdate(sql, obj);
        return count;
    }


    /**
     * 修改
     * @param objects
     * @return
     */
    public int up(List<Object> objects) {
        String sql="update book set bname=?,bauthor=?,bpublish=?,bprice=?,bdate=?,bsbn=? where bid=? ";
        int count= JdbcUtil.executeUpdate(sql, objects);
        return count;
    }

    /**
     * 获取
     * @param id
     * @return
     */
    public Book getId(String id) {
        String sql="select * from book where bid="+id;
        List<Book> users= JdbcUtil.executeQuery(sql,null,Book.class);
        return users.get(0);
    }

    /**
     * 删除
     * @param objects
     * @return
     */
    public int del(List<Object> objects) {
        String sql="delete from book  where bid=? ";
        int count=JdbcUtil.executeUpdate(sql, objects);
        return count;
    }


}
